//
//  UserStatus.m
//  Statz
//
//  Created by Dave MacLachlan on 07/07/26.
//
// Copyright 2007 Google Inc.
// 
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License.  You may obtain a copy
// of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
// License for the specific language governing permissions and limitations under
// the License.
//

#import "UserStatusSource.h"
#import "StatzProtocols.h"
#import "Status.h"
#import "StatzSourceTokenCell.h"

const int kStatzUserStatusCoderVersion = 1;
static NSString *const kStatzUserStatusCoderVersionKey = @"kStatzUserStatusCoderVersionKey";
static NSString *const kStatzUserStatusSourceNameKey = @"kStatzUserStatusSourceNameKey";

@implementation UserStatusSource

- (id)init {
  if ((self = [super init])) {
    name_ = NSLocalizedString(@"New Value", @"New Source Name");
    [self addStatus:[[[Status alloc] init] autorelease]];
  }
  return self;
}

- (id)initWithName:(NSString*)name {
  if ((self = [super init])) {
    name_ = [name retain];
  }
  return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
  if ((self = [super initWithCoder:aDecoder])) {
    int version = [aDecoder decodeInt32ForKey:kStatzUserStatusCoderVersionKey];
    if (version != kStatzUserStatusCoderVersion) {
      [self release];
      return nil;
    }
    name_ = [[aDecoder decodeObjectForKey:kStatzUserStatusSourceNameKey] retain];
  }
  return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
  [super encodeWithCoder:aCoder];
  [aCoder encodeInt32:kStatzUserStatusCoderVersion forKey:kStatzUserStatusCoderVersionKey];
  [aCoder encodeObject:name_ forKey:kStatzUserStatusSourceNameKey];
}

- (void)dealloc {
  [name_ release];
  [super dealloc];
}

- (NSString*)name {
  return [[name_ copy] autorelease];
}

- (void)setName:(NSString*)value {
  [name_ autorelease];
  name_ = [value retain];
  [(StatzSourceTokenCell*)[attachment_ attachmentCell] setTitle:name_];
}


@end

@implementation DefaultStatusSource : UserStatusSource
- (id)init {
  NSString *defaultStatusString = NSLocalizedString(@"My Status", "Default Status String");
  self = [super initWithName:defaultStatusString];
  return self;
}

- (BOOL)isNameEditable {
  return NO;
}
@end
